package com.slz3.business.module.drawManagement.draIntegralLottery.sync;

import com.slz3.business.frame.module.DomainEntity;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.drawManagement.draIntegralLottery.domain.DraIntegralLottery;
import com.slz3.business.module.drawManagement.draIntegralLottery.enums.DraIntegralLotteryStatus;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

public class DraIntegralLotteryPGToRedisSync {

    public static void sync(RedisTemplate redisTemplate, List<DomainEntity> validAll) {
        // （进行中-活动倒计时 | 往期-开奖倒计时 | 即将开始-开始倒计时）
        // 强转
        List<DraIntegralLottery> domainList = validAll.stream().map(item -> (DraIntegralLottery) item).collect(toList());

        //
        LocalDateTime now = LocalDateTime.now();

        // 即将开始
        List<UUID> notStartingDomainList = domainList.stream()
                // now < 开始
                .filter(item -> now.isBefore(item.getSerGoodsDrawStartDate())
                        && item.getSerGoodsDrawStartDate() != null
                )
                .sorted(comparing(DraIntegralLottery::getSerGoodsDrawStartDate))
                .map(DraIntegralLottery::getUuid)
                .collect(toList());
        // 进行中
        List<UUID> startingDomainList = domainList.stream()
                // 开始 < now < 开奖
                .filter(item -> now.isAfter(item.getSerGoodsDrawStartDate())
                        && now.isBefore(item.getSerGoodsDrawOpenDate())
                        && item.getSerGoodsDrawOpenDate() != null
                )
                .sorted(comparing(DraIntegralLottery::getSerGoodsDrawOpenDate))
                .map(DraIntegralLottery::getUuid)
                .collect(toList());
        // 往期
        List<UUID> passedDomainList = domainList.stream()
                // now > 开奖
                .filter(item -> now.isAfter(item.getSerGoodsDrawOpenDate()))
                .sorted(comparing(DraIntegralLottery::getSerGoodsDrawOpenDate).reversed())
                .map(DraIntegralLottery::getUuid)
                .collect(toList());

        //
        String notStartingKey = RK.CONFIG_DOMAIN__DRA_INTEGRAL_LOTTERY__STATUS__$STATUS$__UUID_LIST
                .replace("${status}", DraIntegralLotteryStatus.NOT_STARTING.name());
        String startingKey = RK.CONFIG_DOMAIN__DRA_INTEGRAL_LOTTERY__STATUS__$STATUS$__UUID_LIST
                .replace("${status}", DraIntegralLotteryStatus.STARTING.name());
        String passedKey = RK.CONFIG_DOMAIN__DRA_INTEGRAL_LOTTERY__STATUS__$STATUS$__UUID_LIST
                .replace("${status}", DraIntegralLotteryStatus.PASSED.name());
        redisTemplate.delete(notStartingKey);
        redisTemplate.delete(startingKey);
        redisTemplate.delete(passedKey);
        if (VE.isNotEmpty(notStartingDomainList))
            redisTemplate.opsForList().rightPushAll(notStartingKey, notStartingDomainList);
        if (VE.isNotEmpty(startingDomainList))
            redisTemplate.opsForList().rightPushAll(startingKey, startingDomainList);
        if (VE.isNotEmpty(passedDomainList))
            redisTemplate.opsForList().rightPushAll(passedKey, passedDomainList);
    }

}
